<script setup lang="ts">
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { SysConfig } from '#/api/system/config';

import { Page, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { message, Modal, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import { configList, configRemove } from '#/api/system/config';

import configModal from './config-modal.vue';
import { querySchema, useColumns } from './data';

const formOptions: VbenFormProps = {
  schema: querySchema(),
};

const gridOptions: VxeGridProps = {
  columns: useColumns(),
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await configList({
          current: page.currentPage,
          size: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  id: 'system-config-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [ConfigModal, modalApi] = useVbenModal({
  connectedComponent: configModal,
});

function handleAdd() {
  modalApi.setData({});
  modalApi.open();
}

async function handleEdit(record: SysConfig) {
  modalApi.setData({ id: record.id });
  modalApi.open();
}

async function handleDelete(row: SysConfig) {
  await configRemove([row.id]);
  message.success({
    content: $t('ui.actionMessage.deleteSuccess', [row.configName]),
  });
  await tableApi.query();
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: SysConfig) => row.id);
  Modal.confirm({
    title: $t('common.tip'),
    okType: 'danger',
    content: $t('ui.actionMessage.deleteConfirm', [ids.length]),
    onOk: async () => {
      await configRemove(ids);
      message.success({
        content: $t('ui.actionMessage.deleteSuccess'),
      });
      await tableApi.query();
    },
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable :table-title="$t('system.config.tableTitle')">
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            @click="handleMultiDelete"
          >
            {{ $t('common.delete') }}
          </a-button>
          <a-button type="primary" @click="handleAdd">
            {{ $t('common.create') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <GhostButton @click.stop="handleEdit(row)">
            {{ $t('common.edit') }}
          </GhostButton>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            :title="$t('common.deleteConfirm')"
            @confirm="handleDelete(row)"
          >
            <GhostButton danger @click.stop="" :disabled="row.status === 'Y'">
              {{ $t('common.delete') }}
            </GhostButton>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <ConfigModal @reload="tableApi.query()" />
  </Page>
</template>
